package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.annotation.AutoFill;
import com.sky.constant.StatusConstant;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.enumeration.OperationType;
import com.sky.mapper.CategoryMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 分页管理业务类
 *
 * @Author mr.chen
 * @Date 2025/5/18 15:10
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    /**
     * 分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public PageResult page(CategoryPageQueryDTO dto) {

        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        List<Category> page = categoryMapper.page(dto.getName(), dto.getType());
        Page<Category> pages = (Page<Category>) page;
        return new PageResult(pages.getTotal(),pages.getResult());
    }

    /**
     * 根据id删除分类
     *
     * @param id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Integer id) {
        // TODO 查询当前分类是否关联了套餐，如果关联了就抛出业务异常
        categoryMapper.delete(id);
    }

    /**
     * 修改分类
     *
     * @param dto
     */
    @Override
    @AutoFill(OperationType.UPDATE)
    @Transactional(rollbackFor = Exception.class)
    public void update(Category dto) {
        categoryMapper.update(dto);
    }

    /**
     * 根据id修改状态
     *
     * @param status
     * @param id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateStatus(Integer status, Integer id) {
        categoryMapper.updateStatus(status,id);
    }

    /**
     * 添加套餐分类或者菜品分类
     *
     * @param dto
     */
    @Override
    @AutoFill(OperationType.INSERT)
    @Transactional(rollbackFor = Exception.class)
    public void add(Category dto) {
        dto.setStatus(StatusConstant.ENABLE);
        categoryMapper.add(dto);
    }

    /**
     * 根据类型查询分类
     *
     * @param type
     * @return
     */
    @Override
    public List<Category> list(Integer type) {
        return categoryMapper.list(type);
    }
}
